package general.appClass;

import general.dbClass.DBCategory;
import general.dbClass.DBExif;
import general.dbClass.DBKeyword;
import general.dbClass.DBPicInfo;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.db4o.ObjectContainer;
import com.db4o.cs.Db4oClientServer;
import com.db4o.cs.config.ClientConfiguration;

import db4oClass.ServerInfo;

public class SystemInfo implements ServerInfo {
	private static HttpServletRequest 	request 		= null;
	private static HttpSession 			session 		= null;
	private static String 				xmlFolderPath 	= "";
	private static Connection 			conn 			= null;
	private static ObjectContainer		container 		= null;
	private static String 				dbUser 			= "mmpphase2ws11";
	private static String 				dbPassword 		= "mmpphase2ws11";
	private static String 				dbUrl 			= "jdbc:oracle:thin:@10.0.107.20:1521:GBIR";
	public static void init(HttpServletRequest request) {
		System.out.println("init systeminfo");
		SystemInfo.request = request;
		SystemInfo.session = request.getSession();
		SystemInfo.xmlFolderPath = request.getServletContext().getRealPath("/")
				+ "xml" + System.getProperty("file.separator");
		SystemInfo.conn = newSqlConn();
	}

	private static Connection newSqlConn() {
		Connection conn = null;
		try {
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
			conn.setAutoCommit(false);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	private static ObjectContainer newDb4oConn() {
		ObjectContainer container = null;
		
		try {
			ClientConfiguration config = Db4oClientServer.newClientConfiguration();	
			
			config.common().objectClass(DBPicInfo.class).objectField("nr").indexed(true);
			config.common().objectClass(DBCategory.class).objectField("nr").indexed(true);
			config.common().objectClass(DBKeyword.class).objectField("nr").indexed(true);	
			config.common().objectClass(DBPicInfo.class).cascadeOnUpdate(true);
			config.common().objectClass(DBCategory.class).cascadeOnUpdate(true);
			config.common().objectClass(DBKeyword.class).cascadeOnUpdate(true);
			
			config.common().objectClass(DBCategory.class).cascadeOnDelete(true);
			config.common().objectClass(DBKeyword.class).cascadeOnDelete(true);
			//config.common().objectClass(DBPicInfo.class).cascadeOnDelete(true);
			config.common().objectClass(DBExif.class);
			
			container = Db4oClientServer.openClient(config,
												HOST,
												PORT, 
												USER, 
												PASS);
				

		}catch(Exception ex) {
			ex.printStackTrace();
		}
		
		return container;
	}

	public static Connection getSqlConn() {
		if (session != null && session.getAttribute("conn") != null) {
			conn = (Connection) session.getAttribute("conn");
			try {
				if (conn.isClosed()) {
					conn = newSqlConn();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			conn = newSqlConn();
		}
		return conn;
	}

	public static Connection getConn(HttpServletRequest request) {
		session = request.getSession();
		return getSqlConn();
	}
	
	public static ObjectContainer getDb4oConn(HttpServletRequest request) {
		session = request.getSession();
		
		if(session != null && session.getAttribute("container") !=null) {
			container = (ObjectContainer) session.getAttribute("container");
		} else {
			container = newDb4oConn();
			session.setAttribute("container", container);
		}
		
		return container;
	}

	public static String getXmlFolderPath() {
		if (xmlFolderPath == null || xmlFolderPath.equals("")) {
			xmlFolderPath = request.getServletContext().getRealPath("/")
					+ "xml" + System.getProperty("file.separator");
		}
		return xmlFolderPath;
	}

	public static String getXmlFolderPath(HttpServletRequest request) {
		if (xmlFolderPath == null || xmlFolderPath.equals("")) {
			xmlFolderPath = request.getServletContext().getRealPath("/")
					+ "xml" + System.getProperty("file.separator");
		}
		return xmlFolderPath;
	}

	public static String getMode() {
		String mode;
		if (session == null || session.getAttribute("mode") == null) {
			mode = "sql";
			session.setAttribute("mode", mode);
		} else {
			mode = (String) session.getAttribute("mode");
		}
		return mode;
	}

	public static String getMode(HttpSession session) {
		SystemInfo.session = session;
		return getMode();
	}


	public static boolean isMini() {
		boolean mini;
		if (session == null || session.getAttribute("mini") == null) {
			mini = false;
			session.setAttribute("mini", mini);
		} else {
			mini = (Boolean) session.getAttribute("mini");
		}
		return mini;
	}
	public static boolean isMini(HttpSession session) {
		SystemInfo.session = session;
		return isMini();
	}
	
}
